<template>
  <el-dialog title="新增员工" :visible.sync="dialogShow" @close="closeDialog">
    <!-- 绘制表单 -->
    <el-form
      v-if="dialogShow"
      :model="formData"
      :rules="formRules"
      ref="form"
      label-width="140px"
    >
      <el-form-item label="姓名" prop="username" size="normal">
        <el-input v-model="formData.username" style="width: 320px"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile" size="normal">
        <el-input v-model="formData.mobile" style="width: 320px"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry" size="normal">
        <!-- <el-input v-model="formData.timeOfEntry"></el-input> -->
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 320px"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" size="normal">
        <!-- <el-input
          v-model="formData.formOfEmployment"
          style="width: 320px"
        ></el-input> -->
        <el-select
          placeholder="请选择"
          v-model="formData.formOfEmployment"
          style="width: 320px"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber" size="normal">
        <el-input v-model="formData.workNumber" style="width: 320px"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName" size="normal">
        <el-input
          @focus="getDepList"
          v-model="formData.departmentName"
          style="width: 320px"
        ></el-input>
        <!-- 部门树 -->
        <el-tree
          v-if="treeShow"
          node-key="id"
          @node-click="nodeClick"
          :data="deptList"
          style="width: 320px"
          :props="{ label: 'name' }"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime" size="normal">
        <!-- <el-input v-model="formData.correctionTime"></el-input> -->
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 320px"
        ></el-date-picker>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
//
import EmployeesEnum from '@/api/constant/employees'
import { getDepartmentListApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import { addEmployeesApi } from '@/api/employees'
export default {
  name: 'AddEmployees',
  data() {
    return {
      dialogShow: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '请选择', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      // 聘用形式
      hireType: EmployeesEnum.hireType,
      // 部门数据存储
      deptList: [],
      // 控制树显示
      treeShow: false
    }
  },
  methods: {
    // 获取部门数据 => 树行图转化
    async getDepList() {
      const { depts } = await getDepartmentListApi()
      this.deptList = tranListToTreeData(depts, '')
      this.treeShow = true
    },
    // 树节点被点击时触发
    nodeClick(val) {
      this.formData.departmentName = val.name
      this.treeShow = false
    },
    async btnOk() {
      // 1.校验表单数据
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      // 2.发请求
      await addEmployeesApi(this.formData)
      // 3.提示成功
      this.$message.success('添加成功')
      // 4.更新列表
      this.$emit('updataList')
      // 5.关闭对话弹框,重置表单(关闭弹窗会有触发close事件)
      this.dialogShow = false
    },
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // ⭐重置表单到初始状态 fprm实例对象clearValidate方法
      // console.log(this.$refs.form)
      this.$refs.form.clearValidate()
      // 解决打开树后 重新打开弹框时树是开着的问题
      this.treeShow = false
    }
  }
}
</script>

<style></style>
